package auctioncentral;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * Encapsulates an auction event.
 * 
 * @author Keenan Fejeran, Andres Rosales (acr)
 * @version 03/03/2014 acr
 *
 */
public class Event {
	
	/** The Name of the Auction **/
	private final String my_name;
	
	/** The Non-profit user who owns this event. **/
	private final String my_user;
	
	/** The phone number of the user who owns this event **/
	private final String my_contact_num;
	
	/** The AuctionCentral user who did intake for this event **/
	private final String my_intake_user;
	
	/** The date of this event year, month, day, hour, minute. **/
	private Calendar my_date;
		
	/** The duration of this event in hours. **/
	private final int my_duration;
	
	/** The anticipated number of items for this event. **/
	private final int my_est_items;
	
	/** The comments for this event. **/
	private final String my_comments;
	
	/** The inventory for this event. **/
	private List<Item> my_items = new ArrayList<Item>();
	
	/** Whether the event has been approved by AuctionCentral or not. **/
	boolean my_approval_status;
	
	
	/**
	 * Default constructor for an event.
	 * 
	 * Note that each event that is created is set to an "unapproved" state until 
	 * an Auction Central employee flags the event as being accepted.
	 * @author Keenan Fejeran, acr
	 * 
	 * @param theUser the Nonprofit user who created the event.
	 * @param theDate the date of the event.
	 */
	public Event(final String the_name, final String the_user, final String the_contact_num, 
			final String the_intake_user, final Calendar the_date, final int the_duration, 
			final int the_est_items, final String the_comments) {
		my_name = the_name;
		my_user = the_user;
		my_contact_num = the_contact_num;
		my_intake_user = the_intake_user;
		my_date = the_date;
		my_duration = the_duration;
		my_est_items = the_est_items;
		my_comments = the_comments;
		
		my_approval_status = false;
		
		//System.out.println(toString());
	}
	
	/**
	 * Returns the nonprofit user who owns this event.
	 * @author Keenan Fejeran
	 * 
	 * @return the nonprofit user who owns this event.
	 */
	public String getUser() {
		return my_user;
	}
	
	/**
	 * Returns the inventory for this event.
	 * @author Keenan Fejeran
	 * 
	 * @return the List of Items for this event.
	 */
	public List<Item> getItems() {
	  
		return my_items;
	}
	
//	/**
//	 * set the item list
//	 */
//	public void setItemList (List<Item> some_item_list) {
//	  my_items = some_item_list;
//	}
//	
	/**
	 * @return
	 */
	public void addItems(Item the_item) {
		int current_id = the_item.getMy_id();
		int current_check = 0;
		
		boolean flag = false;
		if(!my_items.isEmpty())
		{
			for(int x = 0; x < my_items.size(); x++)//Item i : my_items)
			{
				if (current_id < my_items.get(x).getMy_id())
				{
					flag = true;
					my_items.add(my_items.indexOf(my_items.get(x)), the_item);
					break;
				}
			}
			if(!flag)
			{
				my_items.add(the_item);
			}
			 
		}
		else
		{
	       my_items.add(the_item);
		}
	}
	
	/**
	 * Returns the date of the event.
	 * @author Keenan Fejeran
	 * 
	 * @return the date of the event.
	 */
	public Calendar getDate() {
		return my_date;
	}
	
	public int getEstItems() {
		return my_est_items;
	}
	
	public int getDuration() {
		return my_duration;
	}
	
	public String getIntakeUser() {
		return my_intake_user;
	}
	
	public String getContactNumber() {
		return my_contact_num;
	}
	/**
	 * Changes the date of the event to the given date.
	 * @author Keenan Fejeran
	 * 
	 * @param theNewDate the desired date of the event.
	 */
	public void setDate(final Calendar theNewDate) {
		my_date = theNewDate;
	}
  
  public boolean getMyApprovalStatus() {
    return my_approval_status;
  }

  public void setMyApprovalStatus(boolean my_approval_status) {
    this.my_approval_status = my_approval_status;
  }
  
  public String getName() {
	  return my_name;
  }
  
  public String getComments() {
	  return my_comments;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
	  StringBuilder sb = new StringBuilder();
	  sb.append(my_name);
	  sb.append('\n');
	  
	  if (my_user != null) {
	  sb.append(my_user);
	  sb.append('\n');
	  }
	  
	  sb.append(my_contact_num);
	  sb.append('\n');
	  
	  if (my_intake_user != null) {
	  sb.append(my_intake_user);
	  sb.append('\n');
	  }
	  
	  sb.append(my_date.toString());
	  sb.append('\n');
	  
	  sb.append(my_duration);
	  sb.append('\n');
	  
	  sb.append(my_est_items);
	  sb.append('\n');
	  
	  sb.append(my_comments);
	  sb.append('\n');
	  
	  return sb.toString();
	  
  }
}
